package com.wa.service.impl;

import cn.hutool.json.JSONUtil;
import com.wa.annotation.AllchipsLogProcessServiceType;
import com.wa.page.BaseLogEntity;
import com.wa.bean.BasicAllchipsLog;
import com.wa.dto.OperatorInfoDto;
import com.wa.pojo.BasicAllchipsLogPojo;
import com.wa.service.AbstractAllchipsLogProcessService;
import com.wa.util.AllchipsLogUtils;
import com.wa.util.CommUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * 日志处理--变更日志
 *
 * @author: Homan Liang
 * @time: 2021/10/22 15:49
 */
@Slf4j
@AllchipsLogProcessServiceType(logCategory = 10)
@Service
public class ChangeAllchipsLogProcessServiceImpl extends AbstractAllchipsLogProcessService {

    /**
     * 日志处理
     * @Author Dawn
     * @Date 2021/10/26 14:36
     */
    @Override
    public void processLog0(Object dataObject, BasicAllchipsLogPojo basicAllchipsLogPojo, OperatorInfoDto operatorInfoDto) {
        BaseLogEntity baseLogEntity = (BaseLogEntity) dataObject;
        //设置操作人信息，如果BaseLogEntity存在，已BaseLogEntity为主
        operatorInfoDto = OperatorInfoDto.process(operatorInfoDto, baseLogEntity.getOperatorInfoDto());

        basicAllchipsLogPojo.setBasicAllchipsLogs(new ArrayList<>());
        BasicAllchipsLog basicAllchipsLog = new BasicAllchipsLog(operatorInfoDto);
        basicAllchipsLogPojo.getBasicAllchipsLogs().add(basicAllchipsLog);

        String beforeDataJson = null;
        if (baseLogEntity != null){
            if (baseLogEntity.getChangeLogInfo() != null){
                basicAllchipsLog.setEntityId(baseLogEntity.getChangeLogInfo().getEntityId());
                basicAllchipsLog.setEntityName(baseLogEntity.getChangeLogInfo().getEntityName());
                basicAllchipsLog.setBefore(baseLogEntity.getChangeLogInfo().getBefore());
                basicAllchipsLog.setAfter(baseLogEntity.getChangeLogInfo().getAfter());
            }
            beforeDataJson = baseLogEntity.getBeforeData();
        }

        // 实体id和实体名称任意为空，就根据配置的注解进行取值
        if (CommUtils.isNull(basicAllchipsLog.getEntityId()) || CommUtils.isEmpty(basicAllchipsLog.getEntityName())){
            AllchipsLogUtils.processLogEntityInfo(dataObject, null, basicAllchipsLog, basicAllchipsLogPojo.getLogModuleEnum());
        }

        // 如果新旧值都为空，就根据配置的注解进行取值
        if (CommUtils.isEmpty(basicAllchipsLog.getBefore()) && CommUtils.isEmpty(basicAllchipsLog.getAfter())){
            // 处理新值
            String after = AllchipsLogUtils.getLogContent(dataObject, null, basicAllchipsLogPojo.getLogModuleEnum());
            basicAllchipsLog.setAfter(after);

            // 处理旧值
            if (CommUtils.isNotEmpty(beforeDataJson)){
                Object beforeDataObject = JSONUtil.toBean(beforeDataJson, dataObject.getClass());
                String before = AllchipsLogUtils.getLogContent(beforeDataObject, null, basicAllchipsLogPojo.getLogModuleEnum());
                basicAllchipsLog.setBefore(before);
            }
        }
    }
}
